using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Data;
using Eldorado.Object;
using Eldorado.Object.Query;
using NUnit.Framework;

using Northwind.DataAccess;

namespace Northwind.NUnit
{
	[TestFixture]
	public class Test1 : BaseTest
	{
		public IObjectContext Context;

		[SetUp]
		public void Setup()
		{
			Context = CreateContext();
			Trace.Listeners.Add(new TextWriterTraceListener(Console.Out, "Console"));
		}

		[TearDown]
		public void TearDown()
		{
			Trace.Listeners.Remove("Console");
		}

		[Test]
		public void TestForeignKeyFactory()
		{
			Orders orders = CreateOrders(Context);
			string ordersOid = orders.ToString();
			Shipper shipper = CreateShipper(Context);
			orders.ShipVia = shipper;
			string shipperOid = shipper.ToString();
			Context.Update();
			Context.LocalStorage.Clear();
			Orders ordersTest = (Orders)Context.Find(typeof(Orders), ordersOid);
			Assert(ordersTest != null);
			Shipper shipperTest = (Shipper)Context.Find(typeof(Shipper), shipperOid);
			Assert(shipperTest != null);
			Assert(ordersTest.ShipVia == shipperTest);
		}

		[Test]
		public void TestOrderDetail()
		{
			Orders orders = CreateOrders(Context);
			Product product = CreateProduct(Context);
			OrderDetail orderdetail = CreateOrderDetail(Context, orders, product);
			Context.Update();
		}

		[Test]
		public void TestCustomer()
		{
			Orders orders = CreateOrders(Context);
			string dt = DateTime.Now.Ticks.ToString();
			Customer customer = CreateCustomer(Context, "C" + dt.Substring(dt.Length - 4) );
			Context.Update();
		}

		[Test]
		public void TestCategory()
		{
			Category category = CreateCategory(Context);
			Context.Update();
		}

		[Test]
		public void TestCustomerDemographic()
		{
			CustomerDemographic cd = CreateCustomerDemographic(Context);
			Context.Update();
		}

		[Test]
		public void TestEmployee()
		{
			Employee employee = CreateEmployee(Context);
			Context.Update();
		}

		[Test]
		public void TestOrder()
		{
			Orders orders = CreateOrders(Context);
			Context.Update();
		}

		[Test]
		public void TestProduct()
		{
			Product product = CreateProduct(Context);
			Context.Update();
		}

		[Test]
		public void TestRegion()
		{
			Region region = CreateRegion(Context);
			Context.Update();
		}

		[Test]
		public void TestShipper()
		{
			Shipper shipper = CreateShipper(Context);
			Context.Update();
		}

		[Test]
		public void TestSupplier()
		{
			Supplier supplier = CreateSupplier(Context);
			Context.Update();
		}

		[Test]
		public void TestTerritory()
		{
			Territory territory = CreateTerritory(Context);
			territory.Region = CreateRegion(Context);
			Context.Update();
		}

		[Test]
		public void TestJoinSansPreload()
		{
			Context.Preload("Orders");
			foreach (Orders orders in Context.LocalStorage.Collect(typeof(Orders)))
			{
				Customer customer = orders.Customer;
				Shipper shipper = orders.ShipVia;
			}
		}

		[Test]
		public void TestJoinAvecPreload()
		{
			Context.Preload("Orders, Orders.Customer, Orders.ShipVia");
			foreach (Orders orders in Context.LocalStorage.Collect(typeof(Orders)))
			{
				Customer customer = orders.Customer;
				Shipper shipper = orders.ShipVia;
			}
		}

		[Test]
		public void TestCustomerDemographicCustomer()
		{
			CustomerDemographic cd = CreateCustomerDemographic(Context);
			Orders orders = CreateOrders(Context);
			string dt = DateTime.Now.Ticks.ToString();
			Customer customer = CreateCustomer(Context, "C" + dt.Substring(dt.Length - 4) );
			cd.Customers.Add(customer);
			Context.Update();
		}
	}
}
